package com.admin.controller;

import com.common.domain.entity.FileManage;
import com.common.domain.entity.JsonResult;
import com.common.domain.entity.ResponseResult;
import com.common.domain.entity.VisitRecord;
import com.common.domain.vo.VisitRecordDto;
import com.common.service.CommentService;
import com.common.service.ConsultationService;
import com.common.service.DepartmentService;
import com.common.service.VisitRecordService;
import com.common.util.FileUtil;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile;

import javax.servlet.http.HttpServletResponse;
import java.io.File;

@RestController
@RequestMapping("system/consultation")
@Slf4j
public class ConsultationController {
    @Autowired
    private ConsultationService consultationService;

    @Autowired
    private CommentService commentService;

    @Autowired
    private VisitRecordService visitRecordService;

    private final String DATATABLECODE = "sys_image_";

    private static String ROOTDIR= "/u01/Server/imgbase" +File.separator;

    private final String STATUS ="1";
    @GetMapping("list")
    public ResponseResult allConsultationList(Integer pageNum, Integer pageSize, String name, String time, String status){
        return consultationService.allConsultationList(pageNum,pageSize,name, time, status);
    }

    @GetMapping("/getUserExchangeLog")
    public ResponseResult getUserExchangeLog(Long id){
        return commentService.getCommentById(id);
    }

    @GetMapping("/getVisitRecode")
    public ResponseResult getVisitRecode(Long id){
        return visitRecordService.getVisitRecode(id);
    }

    @PostMapping("/addVisitRecord")
    public ResponseResult addVisitRecord( VisitRecordDto dto){
        String dirName = "consultation_img";
        String filePath = "";
        if(!dto.getImg().isEmpty()){
            JsonResult<FileManage> fileManageJsonResult = FileUtil.uploadFileManageUtil(dto.getImg(), 0L, dirName, DATATABLECODE, 0);
            if(fileManageJsonResult.isSuccess()){
                FileManage fileManage = (FileManage) fileManageJsonResult.getData();
                filePath = fileManage.getFilePath();

            }
        }
        VisitRecord visitRecord = new VisitRecord();
        visitRecord.setConsultationId(dto.getId());
        visitRecord.setDescribe(dto.getDescribe());
        visitRecord.setDrugInformation(dto.getDrugInformation());
        visitRecord.setFilepath(filePath);
        visitRecordService.insetData(visitRecord);

        //修改状态
        consultationService.updateStatus(dto.getId(), STATUS);
        return ResponseResult.okResult();
    }

    @RequestMapping(value = "/download", method = RequestMethod.GET)
    public void  download(HttpServletResponse response, String fileSavePath){
        fileSavePath = fileSavePath.replace("＼", File.separator);
            String dirName= ROOTDIR;
            String filePath = dirName + fileSavePath;
            try{
                File file = new File(filePath);
                if(! file.exists()) {
                }
            }catch (Exception e){

            }
            FileUtil.downLoad(response,filePath);

    }



}
